﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;
using MeTalker.Aide.Mode;
using System.IO;
using Microsoft.Win32;

namespace MeTalker.Aide.Forms
{
    internal partial class frmSetting : MeForm
    {
        public frmSetting()
        {
            InitializeComponent();
            this.dataGridView.CellContentClick+=new DataGridViewCellEventHandler(dataGridView_CellContentClick);
            //this.meTabControlEx1.Appearance = TabAppearance.Buttons;
            //this.meTabControlEx1.ArrowColor = Color.FromArgb(0, 0x4f, 0x7d);
            //this.meTabControlEx1.BackColor = Color.FromArgb(210, 0xe9, 0xf5);
            //this.meTabControlEx1.BaseColor = SystemColors.Control;
            this.dataGridView.AutoGenerateColumns = false;
            //this.meTabControlEx1.MonseMoveSelect = false;
            InitCheckBox();
            BindData();
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1) //单元格当然要排除列头
            {
                // DataGridViewCell cell = this.dataGridView[0, e.RowIndex];
                Account account = this.dataGridView.CurrentRow.DataBoundItem as Account;
                //也要判断当前点击单元格的名称是否是Link列的名称
                if (this.dataGridView.Columns[e.ColumnIndex].Name == "edit" && account != null)
                {
                    List<Account> lists = dataGridView.DataSource as List<Account>;
                    if (lists != null)
                    {
                        DialogResult result = new frmAddLicense(account).ShowDialog();
                        if (result == DialogResult.Yes)
                        {
                            dataGridView.DataSource = DataFactory.Accounts;
                        }
                    }
                }
                else if (this.dataGridView.Columns[e.ColumnIndex].Name == "delete" && account != null)
                {
                    // this.dataGridView.Rows.RemoveAt(e.RowIndex);
                    DataFactory.Accounts.Remove(account);
                    BindData();
                }
                else if (this.dataGridView.Columns[e.ColumnIndex].Name == "def" && account != null)
                {
                    foreach (var temp in DataFactory.Accounts) {
                        temp.Default = false;
                    }
                    account.Default = true;
                    BindData();
                }
            }
        }


        void InitCheckBox()
        {
            this.chkAutoReply.Checked = DataFactory.SystemConfig.IsAutoReply;
            txtLine.Text = DataFactory.SystemConfig.Line.ToString();
            this.chkSelfLearn.Checked = DataFactory.SystemConfig.SelfLearn;
            chkUsezBrowser.Checked = DataFactory.SystemConfig.IsUsezBrowser;
            chkAutoClickTab.Checked = DataFactory.SystemConfig.IsMoveAutoClickTab;
            chkIsMenuGuideNewLine.Checked = DataFactory.SystemConfig.IsMenuGuideNewLine;
            chkIsHiddenPlatform.Checked = DataFactory.SystemConfig.IsHiddenPlatform;
            chkPriorShort.Checked = DataFactory.SystemConfig.IsPriorShort;
            chkQuerySendExpress.Checked = DataFactory.SystemConfig.IsQuerySendExpress;
            chkCheckSendExpress.Checked = DataFactory.SystemConfig.IsCheckSendExpress;
            chkBillHeader.Checked = DataFactory.SystemConfig.IsBillHeader;
            chkCheckCustomMsg.Checked = DataFactory.SystemConfig.IsCheckCustomMsg;
            chkCheckProperty.Checked = DataFactory.SystemConfig.IsCheckProperty;
            txtQuerySalutation.Text = DataFactory.SystemConfig.QuerySalutation;
            txtNotQuerySalutation.Text = DataFactory.SystemConfig.NotQuerySalutation;
            txtCheckSalutation.Text = DataFactory.SystemConfig.CheckSalutation;
            txtLetterTip.Text = DataFactory.SystemConfig.LetterTip;
            txtQueryContent.Text = DataFactory.SystemConfig.QueryContent;
            chkAutoRun.Checked = DataFactory.SystemConfig.AutoRun;
            chkSeriesThreeError.Checked = DataFactory.SystemConfig.IsSeriesThreeError;
            chkAutoAddQueryMenu.Checked = DataFactory.SystemConfig.IsAutoAddQueryMenu;
            chkAutoDownloadRemark.Checked = DataFactory.SystemConfig.IsAutoDownloadRemark;
            chkIsRemarkAppend.Checked = DataFactory.SystemConfig.IsRemarkAppend;
            chkMulitOrderRemark.Checked = DataFactory.SystemConfig.IsMulitOrderRemark;
            chkAutoAddAccount.Checked = DataFactory.SystemConfig.IsAutoAddAccount;
            chkAutoCurrDate.Checked = DataFactory.SystemConfig.IsAutoAddCurrDate;
            chkAutoECustom.Checked = DataFactory.SystemConfig.IsAutoECustom;
            chkHiddenLicence.Checked=DataFactory.SystemConfig.IsHiddenLicence ;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataFactory.SystemConfig.IsHiddenLicence = chkHiddenLicence.Checked;
            DataFactory.SystemConfig.IsAutoECustom = chkAutoECustom.Checked;
            DataFactory.SystemConfig.IsMulitOrderRemark = chkMulitOrderRemark.Checked;
            DataFactory.SystemConfig.IsAutoAddCurrDate=chkAutoCurrDate.Checked ;
            DataFactory.SystemConfig.IsAutoAddAccount = chkAutoAddAccount.Checked;
            DataFactory.SystemConfig.IsRemarkAppend = chkIsRemarkAppend.Checked;
            DataFactory.SystemConfig.IsAutoDownloadRemark = chkAutoDownloadRemark.Checked;
            DataFactory.SystemConfig.IsAutoAddQueryMenu = chkAutoAddQueryMenu.Checked;
            DataFactory.SystemConfig.IsSeriesThreeError = chkSeriesThreeError.Checked;
            DataFactory.SystemConfig.AutoRun = chkAutoRun.Checked;
            DataFactory.SystemConfig.LetterTip = txtLetterTip.Text;
            DataFactory.SystemConfig.QueryContent = txtQueryContent.Text;
            DataFactory.SystemConfig.IsCheckProperty = chkCheckProperty.Checked;
            DataFactory.SystemConfig.IsMenuGuideNewLine = chkIsMenuGuideNewLine.Checked;
            DataFactory.SystemConfig.IsHiddenPlatform = chkIsHiddenPlatform.Checked;
            DataFactory.SystemConfig.IsMoveAutoClickTab = chkAutoClickTab.Checked;
            DataFactory.SystemConfig.IsAutoReply = this.chkAutoReply.Checked;
            DataFactory.SystemConfig.Line = int.Parse(txtLine.Text) > 10 ? 5 : int.Parse(txtLine.Text);
            DataFactory.SystemConfig.SelfLearn = this.chkSelfLearn.Checked;
            DataFactory.SystemConfig.IsUsezBrowser = chkUsezBrowser.Checked;
            DataFactory.SystemConfig.IsPriorShort = chkPriorShort.Checked;
            DataFactory.SystemConfig.IsQuerySendExpress = chkQuerySendExpress.Checked;
            DataFactory.SystemConfig.IsCheckSendExpress = chkCheckSendExpress.Checked;
            DataFactory.SystemConfig.IsBillHeader = chkBillHeader.Checked;
            DataFactory.SystemConfig.IsCheckCustomMsg = chkCheckCustomMsg.Checked;
            DataFactory.SystemConfig.QuerySalutation = txtQuerySalutation.Text;
            DataFactory.SystemConfig.NotQuerySalutation = txtNotQuerySalutation.Text;
            DataFactory.SystemConfig.CheckSalutation = txtCheckSalutation.Text;
            SerializerFactory.ToAppKeyFile();

            if (DataFactory.SystemConfig.AutoRun)
            {
                SetAutoStartup();
            }
            else {
                SetCancleAutoStartup();
            }
            Close();
        }

        private void btnResume_Click(object sender, EventArgs e)
        {
            DataFactory.SystemConfig.IsHiddenLicence = false;
            DataFactory.SystemConfig.IsAutoECustom = false;
            DataFactory.SystemConfig.IsMulitOrderRemark = true;
            DataFactory.SystemConfig.IsAutoAddCurrDate = false;
            DataFactory.SystemConfig.IsAutoAddAccount = false;
            DataFactory.SystemConfig.IsRemarkAppend = true;
            DataFactory.SystemConfig.IsAutoDownloadRemark = true;
            DataFactory.SystemConfig.IsAutoAddQueryMenu = true;
            DataFactory.SystemConfig.IsSeriesThreeError = true;
            DataFactory.SystemConfig.AutoRun = false;
            DataFactory.SystemConfig.LetterTip = "亲，查件机器人乐乐为你服务，您的快递流转信息如下：";
            DataFactory.SystemConfig.QueryContent = "查件";
            DataFactory.SystemConfig.IsCheckProperty = false;
            DataFactory.SystemConfig.IsMenuGuideNewLine = false;
            DataFactory.SystemConfig.IsHiddenPlatform = true;
            DataFactory.SystemConfig.IsMoveAutoClickTab = true;
            DataFactory.SystemConfig.IsAutoReply = true;
            DataFactory.SystemConfig.Line = 5;
            DataFactory.SystemConfig.SelfLearn = true;
            DataFactory.SystemConfig.IsUsezBrowser = true;
            DataFactory.SystemConfig.IsPriorShort = false;
            DataFactory.SystemConfig.IsQuerySendExpress =false;
            DataFactory.SystemConfig.IsCheckSendExpress = false;
            DataFactory.SystemConfig.IsBillHeader = false;
            DataFactory.SystemConfig.IsCheckCustomMsg = false;
            DataFactory.SystemConfig.QuerySalutation = "亲，您的快件状态如下：";
            DataFactory.SystemConfig.NotQuerySalutation = "亲，您最近三个月没有在本店购物，因而没有快件信息哦~";
            DataFactory.SystemConfig.CheckSalutation = "亲，让我们核对一下邮址吧，您的邮址及相关信息是：";
            InitCheckBox();
        }

        void SetCancleAutoStartup()
        {
            RegistryKey key1 = Registry.LocalMachine;
            RegistryKey key2 = key1.CreateSubKey("SOFTWARE");
            RegistryKey key3 = key2.CreateSubKey("Microsoft");
            RegistryKey key4 = key3.CreateSubKey("Windows");
            RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
            RegistryKey key6 = key5.CreateSubKey("Run");
            //如果要取消的话就将key6.SetValue("myForm",exeDir);改成  
            key6.SetValue("jlRun", false);
        }
        
        void SetAutoStartup()
        {
            //string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
            //System.IO.File.Copy("应用程序路径(包括程序名)", StartupPath + "MeTalker.exe", true);

            //获得文件的当前路径  
           // string dir = Directory.GetCurrentDirectory();
            //获取可执行文件的全部路径  
            //string exeDir = dir + @"\MeTalker.exe";
            string exeDir = FileFactory.BaseDirectory + @"\SuperTalker.exe";
            //获取Run键  
            RegistryKey key1 = Registry.LocalMachine;
            RegistryKey key2 = key1.CreateSubKey("SOFTWARE");
            RegistryKey key3 = key2.CreateSubKey("Microsoft");
            RegistryKey key4 = key3.CreateSubKey("Windows");
            RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
            RegistryKey key6 = key5.CreateSubKey("Run");
            //在Run键中写入一个新的键值  
            key6.SetValue("jlRun", exeDir);
            key6.Close();
        }

        private void txtLine_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < (char)46 || e.KeyChar > (char)57) && (e.KeyChar != (char)8 || e.KeyChar == (char)47))
            {
                e.Handled = true;
            }
            else
                e.Handled = false;

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DialogResult result = new frmAddLicense().ShowDialog();
            if (result == DialogResult.Yes)
            {
                dataGridView.DataSource = null;
                dataGridView.DataSource = DataFactory.Accounts;
            }
        }
        
        void BindData()
        {
            dataGridView.DataSource = null;
            dataGridView.DataSource = DataFactory.Accounts;
        }
 
        private void frmSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            //SerializerFactory.ToAppKeyFile();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            UtilHelper.RunBrowser(UtilHelper.HELP_BASE_URL + "57.html");
        }
    }
}